package instance

import (
	"cloud_android_backend/api/common"
	"cloud_android_backend/pkg/middleware/authorization"
	model "cloud_android_backend/pkg/models/users"
	"cloud_android_backend/pkg/modules/instances"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	rb "modules.tenxcloud.com/common/middleware"
	"net/http"
)

type TransferTextParam struct {
	Udid string `json:"udid"`
	Text string `json:"text"`
}

func TransferText(c *gin.Context) {
	li, exists := c.Get(authorization.LoginUserKey)
	if !exists {
		c.AbortWithStatusJSON(common.ErrorResponse("get user from content failed"))
		return
	}
	current := li.(*model.User)

	body, _ := c.Get(rb.RequestBodyKey)
	transferParam := TransferTextParam{}
	if err := json.Unmarshal(body.([]byte), &transferParam); err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	res, err := instances.TransferText(transferParam.Udid, transferParam.Text, current.UserID)
	if err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(res))

}
